﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UDPServer
{
    class Program
    {
        static int port = 7777;
        static Encoding enc = Encoding.UTF8;

        static void Main(string[] args)
        {
            UdpClient sender = new UdpClient();
            try
            {
                sender.Connect("localhost", port);
                byte[] data = enc.GetBytes("Hello World!");
                sender.Send(data, data.Length);
                sender.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error : " + e);
            }
        }

        static void Send(IPEndPoint ipEndPoint, byte[] data)
        {
            UdpClient sender = new UdpClient();
            try
            {
                sender.Send(data, data.Length, ipEndPoint);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error : " + e.ToString());
            }
        }

        static byte[] Receive(ref IPEndPoint ipEndPoint)
        {
            byte[] data = null;
            UdpClient receivingUdpClient = new UdpClient(port);
            try
            {
                data = receivingUdpClient.Receive(ref ipEndPoint);
                Console.WriteLine("{0}: {1}", ipEndPoint, enc.GetString(data));
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e);
            }
            return data;
        }
    }
}
